package tigroids.timer;

import java.util.ArrayList;

import tigroids.timer.SingleTimer.AlarmDuration;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class EditTimerActivity extends Activity {

	private TextView titleView;
	private NumberPicker minutesNumberPicker;
	private NumberPicker secondsNumberPicker;
	private SingleTimer selectedTimer;
	private SeekBar seekbar;
	TextView alarmDurationText;

	private Integer timerId = -1;
	ArrayList<RadioButton> soundRadioGroup = new ArrayList<RadioButton>();;
	public static final int numberOfSoundOptions = 4;

	public TimerApplication getTimerApplication() {
		return (TimerApplication) super.getApplication();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_timer);

		timerId = (Integer) this.getIntent().getExtras()
				.get(TimerApplication.parameterNameTimerToEdit);
		selectedTimer = getTimerApplication().getTimer(timerId);

		minutesNumberPicker = (NumberPicker) findViewById(R.id.minPicker);
		minutesNumberPicker.setCurrent(selectedTimer.getMinutes());

		secondsNumberPicker = (NumberPicker) findViewById(R.id.secPicker);
		secondsNumberPicker.setCurrent(selectedTimer.getSeconds());

		titleView = (TextView) findViewById(R.id.editTitleTxt);
		titleView.setText(selectedTimer.getTitle());
		/* seek bar */
		alarmDurationText = (TextView) findViewById(R.id.seekBarText);
		alarmDurationText.setText(selectedTimer.getDuration().getText());
		seekbar = (SeekBar) findViewById(R.id.seekBar);		
		seekbar.setMax(AlarmDuration.values().length - 1);
		seekbar.setProgress(selectedTimer.getDuration().getId());
		seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				alarmDurationText.setText(AlarmDuration.getAlarmDurationById(progress).getText());
			}

			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			public void onStopTrackingTouch(SeekBar seekBar) {
			}
		});
		/* btn save */
		Button buttonSave = (Button) findViewById(R.id.btnSave);
		buttonSave.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Log.i(TimerApplication.logAppId,
						"EditTimerActivity::SaveButton.OnClick");

				selectedTimer.setTitle(titleView.getText().toString());
				selectedTimer.setTime(minutesNumberPicker.getCurrent(),
						secondsNumberPicker.getCurrent());

				selectedTimer.setSoundOptionId(getCheckedSoundOptionId());
				selectedTimer.setDuration(AlarmDuration.getAlarmDurationById(seekbar.getProgress()));

				((TimerApplication) getApplication()).saveTimer(selectedTimer);

				Intent intent = new Intent(EditTimerActivity.this, MainTimerActivity.class);
				intent.putExtra(TimerApplication.parameterNameCommand, MainTimerActivity.ON_INTENT_SAVE);
				intent.putExtra(TimerApplication.parameterNameTimerToEdit, timerId);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
			}
		});

		/* btn save&start */
		Button buttonSaveAndStart = (Button) findViewById(R.id.btnStartSave);
		buttonSaveAndStart.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				selectedTimer.setTitle(titleView.getText().toString());
				selectedTimer.setTime(minutesNumberPicker.getCurrent(),
						secondsNumberPicker.getCurrent());

				selectedTimer.setSoundOptionId(getCheckedSoundOptionId());
				selectedTimer.setDuration(AlarmDuration.getAlarmDurationById(seekbar.getProgress()));


				((TimerApplication) getApplication()).saveTimer(selectedTimer);

				Intent intent = new Intent(EditTimerActivity.this,
						MainTimerActivity.class);
				intent.putExtra(TimerApplication.parameterNameCommand,
						MainTimerActivity.ON_INTENT_SAVE_AND_START);
				intent.putExtra(TimerApplication.parameterNameTimerToEdit,
						timerId);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
			}
		});

		/* btn cancel */
		Button buttonCancel = (Button) findViewById(R.id.btnCancel);
		buttonCancel.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(EditTimerActivity.this,
						MainTimerActivity.class);
				intent.putExtra(TimerApplication.parameterNameCommand,
						MainTimerActivity.ON_INTENT_CANCEL);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
			}
		});

		// Combine Radio options to one Radio Buttons Group
		for (int cnt = 0; cnt < numberOfSoundOptions; cnt++) {
			createSoundRadioButtonForId(cnt);
		}

		// set saved sound option radio button
		if (selectedTimer.getSoundOptionId() >= 0) {
			setCheckedSoundOptionById(selectedTimer.getSoundOptionId());
		} else {
			// or set default sound option (corresponding to timer id)
			setCheckedSoundOptionById(selectedTimer.getId());
		}
	}

	private void createSoundRadioButtonForId(int id) {
		int radioButtonViewId = -1;
		switch (id) {
		case 0:
			radioButtonViewId = R.id.soundOption0;
			break;
		case 1:
			radioButtonViewId = R.id.soundOption1;
			break;
		case 2:
			radioButtonViewId = R.id.soundOption2;
			break;
		case 3:
			radioButtonViewId = R.id.soundOption3;
			break;

		}
		RadioButton option = (RadioButton) findViewById(radioButtonViewId);
		option.setId(id);
		option.setOnClickListener(soundOptionOnClickListener);
		soundRadioGroup.add(option);
	}

	RadioButton.OnClickListener soundOptionOnClickListener = new RadioButton.OnClickListener() {
		@Override
		public void onClick(View v) {
			setCheckedSoundOptionById(v.getId());
		}
	};

	private void setCheckedSoundOptionById(int checkedId) {
		for (RadioButton rb : soundRadioGroup) {
			if (rb.getId() == checkedId) {
				rb.setChecked(true);
			} else {
				rb.setChecked(false);
			}
		}
	}

	private int getCheckedSoundOptionId() {
		int cnt = 0;
		for (RadioButton rb : soundRadioGroup) {
			if (rb.isChecked()) {
				break;
			} else {
				cnt++;
			}
		}
		return cnt;
	}
}
